package atguigu.controller;

import org.springframework.ai.chat.memory.ChatMemory;
import org.springframework.ai.chat.memory.ChatMemoryRepository;
import org.springframework.ai.chat.memory.MessageWindowChatMemory;
import org.springframework.ai.chat.memory.repository.jdbc.JdbcChatMemoryRepository;
import org.springframework.ai.chat.messages.UserMessage;
import org.springframework.ai.chat.model.ChatResponse;
import org.springframework.ai.chat.prompt.Prompt;
import org.springframework.ai.deepseek.DeepSeekChatModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.crypto.interfaces.PBEKey;

@RestController
public class MemoryController {
    //    @Autowired
//    ChatMemoryRepository chatMemoryRepository;

    @Autowired
    public JdbcChatMemoryRepository chatMemoryRepository;

    public ChatMemory chatMemory = MessageWindowChatMemory.builder()
            .chatMemoryRepository(chatMemoryRepository)
            .maxMessages(10)
            .build();

    private final DeepSeekChatModel chatModel;

    @Autowired
    public MemoryController(DeepSeekChatModel chatModel) {
        this.chatModel = chatModel;
    }
    @GetMapping("/ai/memory")
    public String chatMemoryRepository(@RequestParam(defaultValue = "007") String conversationId,@RequestParam(defaultValue = "我叫吕杰") String message) {
        UserMessage userMessage1 = new UserMessage(message);
        chatMemory.add(conversationId, userMessage1);
        ChatResponse response1 = chatModel.call(new Prompt(chatMemory.get(conversationId)));
        chatMemory.add(conversationId, response1.getResult().getOutput());
        return response1.getResult().getOutput().getText();
    }

}
